<?php

namespace app\admin\controller;

use think\Db;

class Message extends Authorized
{

    public function index()
    {
        return $this->fetch();
    }
    public function getList()
    {
        $get_data=$this->request->get();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $keywords = input('keywords');
        $where=[];

        if (!empty($keywords)){
            $where['name|tel|email']=['like','%'.$keywords.'%'];
        }

        $count=Db::table('message')->where($where)->count();
        $rows=Db::table('message')->where($where)
            ->order('sort asc,created_at desc')
            ->page($page, $limit)
            ->select();
        return json(['code' =>0,'count' =>$count,'msg' =>'','data'=>$rows]);
    }


    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $post_data['updated_at'] = time();
            $result =Db::table('message')->where('id', $post_data['id'])->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }


        $article=Db::table('message')
            ->where('id' , input('id'))
            ->find();

        $this->assign('gg', $article);

       return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('message')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }
}